; sidload.asm
; SID Emu start code - loading & initialisation

	.include "vicrom.inc"

	.import boot
	.importzp ptr
	
	.import __RAM_START__

	.segment "BASIC"

; Startup Basic program "2011 SYS prolog"
			.word	basprg
basprg:		.word	@end
			.word	2011
			.byte	$9E
			.byte	<(prolog / 1000 .mod 10) + $30
			.byte	<(prolog / 100 .mod 10) + $30
			.byte	<(prolog / 10 .mod 10) + $30
			.byte	<(prolog / 1 .mod 10) + $30
			.byte	0
@end:		.word	0

	.segment "BOOT"

main_name:	.byte "sidemu.bin"
	main_len = * - main_name

prolog:		; print "loading..."
			lda #<load_msg
			ldy #>load_msg
			jsr STROUT
			lda $BA
			bne @do_load
			lda #8
			sta $BA
@do_load:	lda #main_len
			ldx #<main_name
			ldy #>main_name
			jsr SETNAM
			lda #<__RAM_START__
			sta ptr
			lda #>__RAM_START__
			sta ptr+1
			lda #1			; LFN
			ldx $BA			; device
			ldy #0			; secondary address
			jsr SETLFS
			jsr OPEN
			ldx #1			; set #1 for input
			jsr CHKIN
			bcs @load_fail
@read_loop:	ldy $90			; check status
			bne @eof
			jsr BASIN
			bcs @load_fail
			ldy #0
			sta (ptr), y
			inc ptr
			bne @read_loop
			inc ptr+1
			bne @read_loop
@eof:		jsr CLRCH
			lda #1
			jsr CLOSE
			jmp boot
			
@load_fail:	lda #1
			jsr CLOSE
			pla
			pla
			lda #<fail_msg
			ldy #>fail_msg
			jmp STROUT

load_msg:	.byte "loading...", 13, 0
fail_msg:	.byte "?load error", 0
